import requests,sys

url = "http://jenkins.xxx.me/metrics/xxx/metrics"

metrics = {}

# 将json数据扁平化
def denormalize(data, keyname):
    for d in data.keys():
        if isinstance(data[d], dict):
            denormalize(data[d], u"%s%s." % (keyname, d))
        else:
            # 内存指标单位为Byte，需转换为MB
            if keyname == "gauges.vm.memory.total.used.":
                metrics[u"%s%s" % (keyname, d)] = data[d]/(pow(1024,2))
            else:
                metrics[u"%s%s" % (keyname, d)] = data[d]

if __name__ == "__main__":
    respones = requests.get(url=url)
    data = respones.json()

    denormalize(data, "")

    if len(sys.argv) > 1:
        print(metrics[str(sys.argv[1])])
    else:
        for m in metrics:
            print(m)

